require 'pathname'
require 'digest/sha1'
module Com
module Googlecode
module VikrantIncubator
  
  #folder1 is base, folder2 is backup whose integrity has to be verified.
  def check_integrity folder1, folder2
    path1 = Pathname.new folder1
    path2 = Pathname.new folder2
    recurse_for_files(folder1) { |f1|
      puts "INDEXING: #{f1}"
      hash1 = Digest::SHA1.hexdigest IO.read f1
      f2 = path2.join Pathname.new(f1).relative_path_from path1
      puts "INDEXING: #{f2}"
      hash2 = Digest::SHA1.hexdigest IO.read f2.to_s
      unless hash1 == hash2
        puts "Integrity FAILED"
        puts "File 1: #{f1}"
        puts "File 2: #{f2}"
        puts ""
        puts "0. Exit"
        puts "1. Continue"
        exit unless gets.to_i == 1
      else
        puts "Hash OK"
      end
    }
  end
  
  #Pass block with argument as file.
  def recurse_for_files folder, &block
    folder = folder.strip.gsub(/\/$/, '')
    Dir[folder + '/*'].each { |f|
      if File.directory? f
        recurse_for_files f, &block
      else
        yield f
      end
    }
  end

end
end
end
